package cn.daniellee.plugin.lm;

import cn.daniellee.plugin.lm.command.LotteryCommand;
import cn.daniellee.plugin.lm.component.ItemGenerator;
import cn.daniellee.plugin.lm.listener.MenuListener;
import cn.daniellee.plugin.lm.listener.PlayerListener;
import cn.daniellee.plugin.lm.storage.JsonStorage;
import cn.daniellee.plugin.lm.storage.Storage;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class LotteryMaster extends JavaPlugin {

	private static final String PACKAGE_VERSION = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];

	private static LotteryMaster instance;

	private String prefix;

	private Storage storage;

	public void onEnable(){
		instance = this;

		if (loadConfig()) {
			getLogger().info(" ");
			getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> LotteryMaster Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
			getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
			getLogger().info(" ");

			Bukkit.getPluginManager().registerEvents(new MenuListener(), this);
			Bukkit.getPluginCommand("lotterymaster").setExecutor(new LotteryCommand());
		}

	}

	public boolean loadConfig() {
		getLogger().info("[LotteryMaster] Loading config...");
		if(!getDataFolder().exists()) getDataFolder().mkdirs();
		saveDefaultConfig();
		storage = new JsonStorage();
		if (storage.initialize()) {
			getLogger().info("[LotteryMaster]Storage initialized.");
		} else {
			getLogger().info(" ");
			getLogger().info("[LotteryMaster]Initializing data store failed, please edit the config and reload the plugin.");
			getLogger().info(" ");
			Bukkit.getPluginManager().disablePlugin(this);
			return false;
		}
		prefix = "&7[&b" + getConfig().getString("prompt-prefix", "LotteryMaster") + "&7] &3: &r";
		ItemGenerator.disableSkullLoad = getConfig().getBoolean("setting.disable-skull-load", false);
		return true;
	}

	@Override
	public void onDisable() {
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> LotteryMaster Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(" ");
	}

	public static LotteryMaster getInstance() {
		return instance;
	}

	public String getPrefix() {
		return prefix;
	}

	public Storage getStorage() {
		return storage;
	}

	public static Class<?> getNMSClass(String nmsClassName) throws ClassNotFoundException {
		return Class.forName("net.minecraft.server." + PACKAGE_VERSION + "." + nmsClassName);
	}

}